WarResultScene = {}
WarResultSceneFunc = {}
WarResultSceneFunc.__index = WarResultSceneFunc
setmetatable(WarResultScene, WarResultSceneFunc)

function onStarAction(node)
  local starConfig = {[1]="star15.png", [2]="star27.png", [3]="star38.png"}

  local eval = WarResultScene.battle:getEvaluate() 
  if eval==nil then return end
  local sp = CCSprite:createWithSpriteFrameName(starConfig[eval])
  --local sp = CCSprite:createWithSpriteFrameName("star38.png")
  node:getParent():addChild(sp)
  sp:setPosition(node:getPosition())
  node:getParent():removeChild(node, true)
end

WarResultSceneFunc.showLoseWnd = function(self, parent)
  print("WarResultSceneFunc.showLoseWnd")
  local layer = CCBReader:load("WarLoseScene.ccbi")
  setObjectExtTable(layer:getChildByTag(1111), self)
  setObjectExtTable(layer:getChildByTag(1020), self)
  setObjectExtTable(layer:getChildByTag(1021), self)
  setObjectExtTable(layer:getChildByTag(1022), self)
  setObjectExtTable(layer:getChildByTag(1023), self)
  setObjectExtTable(layer:getChildByTag(1024), self)
  setObjectExtTable(layer:getChildByTag(1025), self)
  self.node = layer 
  self.parent = parent
  --self.parent.node:addChild(layer)
  MainScene.scene:addChild(layer)
end

WarResultSceneFunc.showWinWndExt = function(self, parent, name, battle)
  print("WarResultSceneFunc.showWinWnd")
  self.battle = battle
  local layer = CCBReader:load("WarWinScene.ccbi")
  setObjectExtTable(layer:getChildByTag(1111), self)
  setObjectExtTable(layer:getChildByTag(1022), self)
  local lb = layer:getChildByTag(1002)
  TypeCast(lb, "CCLabelTTF"):setString(string.format("您击败了%s", name))
  
  TypeCast(layer:getChildByTag(1003), "CCLabelTTF"):setString(string.format("我方损失：%d兵力", battle:getSoliderLost(1)))
  TypeCast(layer:getChildByTag(1004), "CCLabelTTF"):setString(string.format("敌方损失：%d兵力", battle:getSoliderLost(2)))
  
  local rewards = battle:getRewardItems()
  for k,v in ipairs(rewards) do
    local bk = layer:getChildByTag(1030+k)
    if v.t=="cash" then
      addSpriteFromBK(layer, 1030+k, "cash2.png")
      addLabelFromBK(layer, 1030+k, tostring(v.v), "Helvetica", 32, ccp(0, -30))
    elseif v.t=="xp" then
      addSpriteFromBK(layer, 1030+k, "exp2.png")
      addLabelFromBK(layer, 1030+k, tostring(v.v), "Helvetica", 32, ccp(0, -30))
    elseif v.t=="item" then
      local itemInfo = UserData.config.models.Item[v.v.id]
      addSpriteFromBK(layer, 1030+k, nameFromPath(itemInfo.asset))
      addLabelFromBK(layer, 1030+k, tostring(v.v.num), "Helvetica", 32, ccp(0, -30))
    elseif v.t=="equip" then
      local equipInfo = UserData.config.models.Equip[v.v.id]
      addSpriteFromBK(layer, 1030+k, nameFromPath(equipInfo.asset))
      addLabelFromBK(layer, 1030+k, tostring(v.v.num), "Helvetica", 32, ccp(0, -30))
    elseif v.t=="reputation" then
      --local itemInfo = UserData.config.models.Item[v.v.id]
      addSpriteFromBK(layer, 1030+k, "shengwang100.png")
      addLabelFromBK(layer, 1030+k, tostring(v.v), "Helvetica", 32, ccp(0, -30))
    end
  end 

  local eval = battle:getEvaluate() 
  print("WarResultSceneFunc.showWinWndExt", eval) 
  if eval~=nil and eval>0 then
    local starConfig = {[1]="star1action.plist", [2]="star2action.plist", [3]="star3action.plist"}
    CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile("star3action.plist")
    local sp = CCSprite:createWithSpriteFrameName("star01.png")
    sp:setPosition(ccp(480,556))
    layer:addChild(sp)
    local ac = ActionUtil.createAction(starConfig[eval], 0.05, "star")
    sp:runAction(ActionUtil.createSeqAction({ac, CCCallFuncN:create(onStarAction)}))
  end
  self.node = layer
  self.parent = parent
  self.name = name
  WarResultScene.layer = layer
  --self.parent.node:addChild(layer)
  MainScene.scene:addChild(layer)
  RenWuGuide.nextStep()
end

WarResultSceneFunc.showWinWnd = function(self, parent, name, exp, cash, eval)
  print("WarResultSceneFunc.showWinWnd")
  local layer = CCBReader:load("WarWinScene.ccbi")
  setObjectExtTable(layer:getChildByTag(1111), self)
  local lb = layer:getChildByTag(1002)
  TypeCast(lb, "CCLabelTTF"):setString(string.format("您击败了%s", name))
  local lb = layer:getChildByTag(1005)
  TypeCast(lb, "CCLabelTTF"):setString(tostring(cash))
  local lb = layer:getChildByTag(1006)
  TypeCast(lb, "CCLabelTTF"):setString(tostring(exp))

  print("ZhanDouSceneFunc.showWinWnd", FightData.json.data.drops)
  if FightData.json.data.drops~=nil then
    for k,v in pairs(FightData.json.data.drops) do
      local equipInfo = UserData.config.models.Equip[v.id]
      local imgName = nameFromPath(equipInfo.asset)
      print("ZhanDouSceneFunc.showWinWnd", imgName, 2000+k)
      addSpriteFromBK(layer, 2000+k, imgName)
    end
  end

  local starConfig = {[1]="star1action.plist", [2]="star2action.plist", [3]="star3action.plist"}
  CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile("star3action.plist")
  local sp = CCSprite:createWithSpriteFrameName("star01.png")
  sp:setPosition(ccp(480,556))
  layer:addChild(sp)
  local ac = ActionUtil.createAction(starConfig[eval], 0.01, "star")
  sp:runAction(ActionUtil.createSeqAction({ac, CCCallFuncN:create(onStarAction)}))

  self.parent = parent
  self.parent.node:addChild(layer)
  WarResultScene.layer = layer
end

WarResultSceneFunc.onLoseWinButton = function(self, node, event)
  print("WarResultSceneFunc.onLoseWinButton")
  node:getParent():getParent():removeChild(node:getParent(), true)
  --self.parent.node:getParent():removeChild(self.parent.warScene.node, true)
  --self.parent.node:getParent():removeChild(self.parent.node, true)
  --self.parent.node:getParent():removeChild(self.parent.node, true)
  local id = node:getTag()
  if id==1020 then
    local l = ZhuangBeiScene.getNode()
    MainScene.scene:addChild(l.node)
    --setTouchEnabledAll(MainScene.node, false)
  elseif id==1021 then
    local l = KeXueYuanScene.getNode()
    MainScene.scene:addChild(l.node)
    --setTouchEnabledAll(MainScene.node, false) 
  elseif id==1022 then
    local l = JunXiaoScene.getNode()
    l.radioMenu:setMenuState(2)
    MainScene.scene:addChild(l.node)
    --setTouchEnabledAll(MainScene.node, false)
  elseif id==1023 then
    local l = ZhuangBeiScene.getNode()
    l.radioMenu:setMenuState(1)
    MainScene.scene:addChild(l.node)
    --setTouchEnabledAll(MainScene.node, false)
  elseif id==1024 then
  elseif id==1025 then
  end
end
WarResultSceneFunc.onSeeAgainButton = function(self, node, event)
--  self.parent:seeAgain() 
  local war = WarScene:getNode(self.name, self.parent, self.battle.battleData)
  --self:updateTable()
  self.parent.node:addChild(war.node)
  node:getParent():getParent():removeChild(node:getParent(), true)
end
WarResultSceneFunc.onCloseWarWinScene = function(self, node, event)
  print("WarResultSceneFunc.onCloseWarWinScene")
  WarResultScene.battle = nil
  --setTouchEnabledAll(self.parent.node, true)
  --node:getParent():getParent():removeChild(node:getParent(), true)
  MainScene.scene:removeChild(self.node, true)
  self.parent:fightEnd()
end
